package com.nuanshui.heatedloan.fund.integration.pay.execute.bizexcute;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.fund.enums.PaymentBizType;
import com.nuanshui.heatedloan.fund.enums.PlatformEnum;
import com.nuanshui.heatedloan.fund.facade.UserFundActionService;
import com.nuanshui.heatedloan.fund.integration.form.CancelPayForm;
import com.nuanshui.heatedloan.fund.integration.form.PayForm;
import com.nuanshui.heatedloan.fund.integration.form.RequestPayForm;
import com.nuanshui.heatedloan.fund.integration.pay.annotation.PayHandlerComponent;
import com.nuanshui.heatedloan.fund.integration.pay.execute.sysexecute.AbstractPayExecute;
import com.nuanshui.heatedloan.fund.model.PayResult;
import com.nuanshui.heatedloan.fund.model.PayResult.PayResultStatusType;
import com.nuanshui.heatedloan.fund.model.UserFundAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 充值支付流程的执行类
 * @param 
 * @author guohao
 * @date 2017年2月16日 下午2:42:29
 * @since $
 */
@PayHandlerComponent(value="rechargePayExecute",bizType=PaymentBizType.RECHARGE)
public class RechargePayExecute extends AbstractPayExecute {

	private Logger Logger = LoggerFactory.getLogger(RechargePayExecute.class);
	
	@Reference(url=GlobalParam.FUND_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version=GlobalParam.FUND_DUBBO_SERVER_VERSION)
    private UserFundActionService userFundActionService;
	
	
	
	@Override
	public String doLoadPayNum(RequestPayForm requestPayForm) {
		String dealNum = userFundActionService.loadRechargeOrderNum(requestPayForm.getMoney()*100,requestPayForm.getUserId());
		return dealNum;
	}

	

	@Override
	public boolean doCancelPay(CancelPayForm payForm) {
		return true;
	}


	/**
	 * 确认支付前执行的校验权限
	 * @param @param payForm
	 * @param @param payResult
	 * @author guohao
	 * @date 2017年1月11日 上午10:09:49
	 * @since $
	 *  
	 * @see com.nuanshui.heatedloan.fund.integration.AbstractPayExecute#checkPrivilege(com.nuanshui.heatedloan.fund.integration.form.PayForm, com.nuanshui.heatedloan.fund.model.PayResult)
	 *
	 */
	@Override
	public void checkPrivilege(PayForm payForm, PayResult payResult) {
		super.checkPrivilege(payForm, payResult);		
		if(payForm.getPayType().equals(PlatformEnum.PLATFORM_YE.getName())) {
			//payResult.setStatus(PayResultStatusType.ERROR_PAY_TYPE);
		}	
		long money = payForm.getPayPrice();
		UserFundAction userFundAction = userFundActionService.loadByDealNum(payForm.getOrderNum());
		if (userFundAction == null) {// 支付的当单号不存在

			payResult.setStatus(PayResultStatusType.ERROR_PAY_NUM);
			return;
		}
		if (userFundAction.getMoney().longValue() != money) {//支付金额有误
			payResult.setStatus(PayResultStatusType.ERROR_PAY_PRICE);
			return ;
		}
		if (userFundAction.getStatus() == 10) {//重复支付
			payResult.setStatus(PayResultStatusType.REPEAT_CONFIRM);
			return ;
		}
		payResult.setUserId(userFundAction.getUserId());
	}

	@Override
	public PayResult doConfirmPay(PayForm payForm,PayResult payResult) {
		
		try {
			 userFundActionService.confirmUserRechange(payForm.getPayPrice().longValue(),
					payForm.getOrderNum(), payForm.getPayType());
		} catch (Exception e) {
			LogUtil.error(Logger, "余额支付确认有误",e);
			payResult.setStatus(PayResultStatusType.BIZ_ERROR);
		}
		return payResult;
	}
	
	/**
	 * 生成账单时候 * -1 了, 所以这里置为负的
	 * @Author: Gonzo
	 * @Date 2017/4/27下午9:09
	 * @since 1.4.2
	 */
	@Override
	public void confirmSuccess(PayForm payForm ,PayResult payResult) {
		payForm.setPayPrice(payForm.getPayPrice() * -1);
	}/**
	 * 处理生成账单需要的一些信息
	 * @param @param payResult
	 * @author guohao
	 * @date 2017年1月11日 上午10:21:46
	 * @since $
	 *
	 */
	@Override
	public void handlerAccountBillInfo(PayResult payResult,PayForm payForm) {
		super.handlerAccountBillInfo(payResult,payForm);
		payResult.setTitle("余额充值");
		payResult.setSubType(1);	
		payResult.setPrice((long)payForm.getPayPrice());
	}
	
}
